{/* Copyright 2025 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License. */}

import {Layout} from '../../src/Layout';
export default Layout;
import {FunctionAPI} from '../../src/FunctionAPI';
import docs from 'docs:@react-aria/i18n';

export const section = 'Utilities';
export const description = 'Wraps Intl.Collator and compares and sorts strings according to the current locale.';


# useCollator

<PageDescription>{docs.exports.useCollator.description}</PageDescription>

## Introduction

`useCollator` wraps a builtin browser [Intl.Collator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator)
object to provide a React Hook that integrates with the i18n system in React Aria. It handles string comparison according to the current locale,
updating when the locale changes, and caching of collators for performance. See the
[Intl.Collator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator) docs for
information.

## Example

This example includes two textfields and compares the values of the two fields using a collator according to the current locale.

```tsx render
'use client';
import React from 'react';
import {useCollator} from 'react-aria';

function Example() {
  let [first, setFirst] = React.useState('');
  let [second, setSecond] = React.useState('');

  let collator = useCollator();
  let result = collator.compare(first, second);

  return (
    <>
      <div>
        <label htmlFor="first-string">First string</label>
        <input
          id="first-string"
          value={first}
          onChange={e => setFirst(e.target.value)} />
        <label htmlFor="second-string">Second string</label>
        <input
          id="second-string"
          value={second}
          onChange={e => setSecond(e.target.value)} />
      </div>
      <p>
        {result === 0
          ? 'The strings are the same'
          : result < 0
            ? 'First comes before second'
            : 'Second comes before first'
        }
      </p>
    </>
  );
}
```

## API

<FunctionAPI function={docs.exports.useCollator} links={docs.links} />